package com.bnyy.medicalHousekeeper.moudle.message.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.bnyy.gbp.customsView.ScaleImageView;
import com.bnyy.medicalHousekeeper.GlideHelper;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.bnyy.message.bean.GroupChatDetail;
import com.bnyy.message.bean.RequestEnterChatGroupResult;

import java.util.HashMap;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/12/9
 **/
public class JoinGroupChatActivity extends BaseActivityImpl {
    @BindView(R.id.iv)
    ScaleImageView iv;
    @BindView(R.id.tv_group_chat_name)
    TextView tvGroupChatName;
    @BindView(R.id.tv_group_chat_leader)
    TextView tvGroupChatLeader;
    @BindView(R.id.tv_operate)
    TextView tvOperate;

    private int groupChatId;
    private GroupChatDetail mGroupChatDetail;

    public static void show(Context context, int groupChatId) {
        Intent intent = new Intent(context, JoinGroupChatActivity.class);
        intent.putExtra("groupChatId", groupChatId);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_join_group_chat;
    }

    @Override
    public String getTitleStr() {
        return " ";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        groupChatId = getIntent().getIntExtra("groupChatId", -1);

        HashMap<String, Object> params = new HashMap<>();
        params.put("id", groupChatId);
        requestManager.request(requestManager.mMessageRetrofitService.getGroupChatDetail(RequestManager.getJsonRequestBody(params)),
                new BaseObserverImpl<GroupChatDetail>(mContext) {
                    @Override
                    public void onSuccess(GroupChatDetail groupChatDetail) {
                        super.onSuccess(groupChatDetail);
                        mGroupChatDetail = groupChatDetail;

                        GlideHelper.setCircleImage(mContext, groupChatDetail.getUser_img(), iv, R.mipmap.icon_default_header_man);

                        tvGroupChatName.setText(groupChatDetail.getGroup_name() + "(" + groupChatDetail.getMemberCount() + "）");
                        tvGroupChatLeader.setText(groupChatDetail.getUsername());
                        tvGroupChatLeader.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                UserHomePageActivity.show(mContext, groupChatDetail.getUser_id(), groupChatDetail.getRole_id());
                            }
                        });

                        tvOperate.setText(groupChatDetail.getStatus_name());
                        setStatus(mGroupChatDetail.getStatus());
                    }
                });
    }

    private void setStatus(int status) {
        switch (status) {
            case 1:
                tvOperate.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        HashMap<String, Object> params = new HashMap<>();
                        params.put("id", groupChatId);
                        requestManager.request(requestManager.mMessageRetrofitService.requestEnterGroupChat(RequestManager.getJsonRequestBody(params)),
                                new BaseObserverImpl<RequestEnterChatGroupResult>(mContext) {
                                    @Override
                                    public void onSuccess(RequestEnterChatGroupResult requestEnterChatGroupResult) {
                                        super.onSuccess(requestEnterChatGroupResult);
                                        HashMap<String, Object> params = new HashMap<>();
                                        params.put("id", groupChatId);
                                        requestManager.request(requestManager.mMessageRetrofitService.getGroupChatDetail(RequestManager.getJsonRequestBody(params)),
                                                new BaseObserverImpl<GroupChatDetail>(mContext) {
                                                    @Override
                                                    public void onSuccess(GroupChatDetail groupChatDetail) {
                                                        super.onSuccess(groupChatDetail);
                                                        mGroupChatDetail = groupChatDetail;

                                                        GlideHelper.setCircleImage(mContext, groupChatDetail.getUser_img(), iv, R.mipmap.icon_default_header_man);

                                                        tvGroupChatName.setText(groupChatDetail.getGroup_name() + "(" + groupChatDetail.getMemberCount() + "）");
                                                        tvGroupChatLeader.setText(groupChatDetail.getUsername());
                                                        tvGroupChatLeader.setOnClickListener(new View.OnClickListener() {
                                                            @Override
                                                            public void onClick(View v) {
                                                                UserHomePageActivity.show(mContext, groupChatDetail.getUser_id(), groupChatDetail.getRole_id());
                                                            }
                                                        });

                                                        tvOperate.setText(groupChatDetail.getStatus_name());
                                                        setStatus(mGroupChatDetail.getStatus());
                                                    }
                                                });
                                    }
                                });
                    }
                });
                break;
            case 2:
                tvOperate.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ChatMessageActivity.show(mContext, mGroupChatDetail);
                        finish();
                    }
                });
                break;
            case 3:
                GradientDrawable bg = new GradientDrawable();
                bg.setColor(ContextCompat.getColor(mContext, R.color.gray_light));
                bg.setCornerRadius(360);
                tvOperate.setBackground(bg);
                tvOperate.setTextColor(ContextCompat.getColor(mContext, R.color.gray_text));
                tvOperate.setEnabled(false);
                break;
            case 4:
                GradientDrawable bg1 = new GradientDrawable();
                bg1.setColor(Color.parseColor("#EAF8EF"));
                bg1.setCornerRadius(360);
                tvOperate.setBackground(bg1);
                tvOperate.setTextColor(ContextCompat.getColor(mContext, R.color.green_theme));
                tvOperate.setEnabled(false);
                break;
        }
    }
}


























